package com.aind.cart.controller;

import com.aind.cart.model.Cart;
import com.aind.cart.service.CartService;
import com.aind.common.constant.SessionContant;
import com.aind.user.model.User;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by lpeng on 2017/2/22.
 */
@Controller
@RequestMapping(value = "/cart")
public class CartController {
    private static Logger logger = Logger.getLogger(CartController.class);

    @Autowired
    private CartService cartService;

    /**
     * 跳转到购物车，并查询用户购物车信息
     * @param request
     * @param session
     * @return
     */
    @RequestMapping(value = "/toCart")
    public ModelAndView toCart(HttpServletRequest request, HttpSession session){
        ModelAndView mav = new ModelAndView("/jsps/cart/list");
        User session_user = (User) session.getAttribute(SessionContant.SESSION_USER);
        if (null != session_user){
            List<Cart> carts = cartService.getCartListByUserId(session_user.getUserId());
            mav.addObject("carts", carts);
        }
        return mav;
    }
}
